package edu.spbu.eclipse.xmledit.andrpodk;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerException;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

public class TransformErrorHandler implements ErrorListener{
	private IResource resource;
	private boolean existErrors;


	public void setResource(IResource resource) {
		this.resource = resource;
	}

	public void error(TransformerException arg0) throws TransformerException {
		try {
			IMarker marker=resource.createMarker(IMarker.PROBLEM);
			marker.setAttribute(IMarker.LINE_NUMBER,arg0.getLocator().getLineNumber());			
			marker.setAttribute(IMarker.MESSAGE, arg0.getMessage());
			marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
			marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
			if(!existErrors){
				existErrors=true;
			}
		} catch (CoreException e) {}
	}

	public void fatalError(TransformerException arg0) throws TransformerException {
		try {
			IMarker marker=resource.createMarker(IMarker.PROBLEM);
			marker.setAttribute(IMarker.LINE_NUMBER,arg0.getLocator().getLineNumber());			
			marker.setAttribute(IMarker.MESSAGE, arg0.getMessage());
			marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
			marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
			if(!existErrors){
				existErrors=true;
			}
		} catch (CoreException e) {}
	}

	public void warning(TransformerException arg0) throws TransformerException {
		try {
			IMarker marker=resource.createMarker(IMarker.PROBLEM);
			marker.setAttribute(IMarker.LINE_NUMBER, arg0.getLocator().getLineNumber());
			marker.setAttribute(IMarker.MESSAGE, arg0.getMessage());
			marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
			marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
		} catch (CoreException e) {}		
	}

	public boolean receivedErrors(){
		return existErrors;				
	}
}
